package com.cjh.wechat.core.service.impl;

import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.cjh.wechat.commom.pojo.Friend;
import com.cjh.wechat.commom.pojo.Team;
import com.cjh.wechat.core.dao.TeamDao;
import com.cjh.wechat.core.service.FriendService;
import com.cjh.wechat.core.service.TeamService;

@Service
public class TeamServiceImpl implements TeamService
{
    @Resource
    private TeamDao       teamDao;
    @Resource
    private FriendService friendService;
    
    @Override
    public void delTeam(String uid, Integer no) throws Exception
    {
        List<Friend> friends = friendService.findByTno(uid, no);
        if (friends.size() == 0)
        {
            teamDao.deleteByPrimaryKey(no);
        }
        else
        {
            Team team = teamDao.selectByPrimaryKey(no);
            team.setName("临时分组");
            teamDao.updateByPrimaryKey(team);
        }
    }
    
    @Override
    public void addTeam(String id, String name) throws Exception
    {
        Team team = new Team();
        team.setUid(id);
        team.setName(name);
        team.setSequence(0);
        teamDao.insertSelective(team);
    }
    
    @Override
    public List<Team> showTeamExcludeFriendId(String id, String friendId)
    {
        Friend friend = friendService.findByUidAndFid(id, friendId);
        List<Team> teams = teamDao.findByUid(id);
        Iterator<Team> iterator = teams.iterator();
        while (iterator.hasNext())
        {
            Team team = iterator.next();
            if (team.getNo().equals(friend.getTno()))
            {
                iterator.remove();
            }
        }
        return teams;
    }
    
    @Override
    public List<Team> showTeamById(String id)
    {
        List<Team> teams = teamDao.findByUid(id);
        return teams;
    }
}
